package com.yongcheon.sms_manager.data.phone_spsm;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.CursorAdapter;

import com.yongcheon.sms_manager.ui.phone_spam.list.PhoneSPAMCurosrAdapter;
import com.yongcheon.sms_manager.vo.PhoneVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-12-24.
 */
public class SpamPhonData extends AbstractPhoneData{

    @Override
    public CursorAdapter getCurosrAdapter(Context context, SQLiteDatabase database) {
        String sql = "Select * from spam_phone order by date desc";
        Cursor cursor = database.rawQuery(sql, null);
        CursorAdapter adapter = new PhoneSPAMCurosrAdapter(context, cursor);
        return adapter;
    }

    @Override
    public void insert(PhoneVO vo, Context context, SQLiteDatabase database) {
        String sql = "Insert into spam_phone (number, voicemail_uri, features, new, formatted_number, numbertype, date, duration, data_usage, presentation, numberlabel, " +
                                                 "geocoded_location, lookup_uri, countryiso, transcription, subscription_component_name, name, matched_number, subscription_id," +
                                                 "photo_id, normalized_number, is_read, type, spam_desc, spam_category) values "+
                "('"+vo.getNumber()+"','"+vo.getVoicemail_uri()+"',"+vo.getFeatures()+","+vo.get_new()+",'"+vo.getFormatted_number()+"','"+vo.getNumbertype()+"',"+vo.getDate()+","+vo.getDuration()+
                ","+vo.getData_usage()+","+vo.getPresentation()+",'"+vo.getNumberlabel()+"','"+vo.getGeocoded_location()+"','"+vo.getLookup_uri()+"','"+vo.getCountryiso()+
                "','"+vo.getTranscription()+"','"+vo.getSubscription_component_name()+"','"+vo.getName()+"','"+vo.getMatched_number()+"','"+vo.getSubscription_id()+"',"+vo.getPhoto_id()+
                ",'"+vo.getNormalized_number()+"',"+vo.getIs_read()+","+vo.getType()+",'"+vo.getSpam_desc()+"','"+vo.getSpam_category()+"')";
        database.execSQL(sql);
    }

    @Override
    public void delete(long id, Context context, SQLiteDatabase database) {
        String sql = "Delete From spam_phone Where _id = "+id;
        database.execSQL(sql);
    }

    @Override
    public void deleteByPhoneNum(String num, Context context, SQLiteDatabase database) {
        String sql =  "Delete From spam_phone Where number = '"+num+"'";
        database.execSQL(sql);
    }

    @Override
    public ArrayList<PhoneVO> select(String poneNum, Context context, SQLiteDatabase database) {
        String sql = null;
        if(poneNum != null){
            sql = "Select * from spam_phone Where number = '" + poneNum+"' order by date desc";
        }else{
            sql = "Select * from spam_phone order by date desc";
        }
        Cursor cursor = database.rawQuery(sql, null);
        return getPhoneListByNum(cursor);
    }
}
